<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <title>$http / $resource</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1">
</head>
<body ng-app="myApp">
<div ng-controller="userCtrl">
    <p>userInfo:{{userInfo}}</p>
    <p>roleInfo:{{roleInfo}}</p>
</div>
<script src="//cdn.bootcss.com/angular.js/1.5.8/angular.min.js"></script>
<script src="//cdn.bootcss.com/angular.js/1.5.8/angular-resource.min.js"></script>
<script>
    var app = angular.module("myApp", ['ngResource']);
    app
            .config(['$httpProvider', function ($httpProvider) {
                $httpProvider.interceptors.push('commonInterceptor');
            }])
            .factory('commonInterceptor', ['$q', '$rootScope', function ($q, $rootScope) {
                var requestInterceptor = {
                    request: function (config) {
                        var deferred = $q.defer();
                        var regStr = /.*\.html/;
                        if (!regStr.test(config.url) && !config.params) {
                            config.params = {};
                        }
                        //设置请求超时
                        if (!config.timeout) {
                            config.timeout = 30000;
                        }

                        deferred.resolve(config);
                        return deferred.promise;
                    },
                    response: function (response) {
                        if (response && response.data.message && response.data.message === 'unauthorized' && !response.data.success) {
                            var deferred = $q.defer();
                            $rootScope.$broadcast('auth-loginRequired', response);
                            return deferred.promise;
                        }
                        return response;
                    },
                    responseError: function (response) {
                        switch (response.status) {
                            case 401:
                                var deferred = $q.defer();
                                $rootScope.$broadcast('auth-loginRequired', response);
                                return deferred.promise;
                            case 403:
                                $rootScope.$broadcast('auth-forbidden', response);
                                break;
                            case -1:
                                $rootScope.$broadcast('offline');
                                break;
                            default:
                                break;
                        }
                        return $q.reject(response);
                    }
                };
                return requestInterceptor;
            }])
            .factory('USER', ['$resource', function ($resource) {
                return $resource("", "", {
                            getInfo: {
                                method: 'GET',
                                headers: {
                                    "content-type": "application/json"
                                },
                                url: "./Json/user.json",
                                isArray: false,
                                timeout: 30000
                            }
                        }
                );
            }])
            .factory('ROLE', ['$http', function ($http) {
                return {
                    getInfo: function () {
                        return $http({
                            method: 'GET',
                            headers: {
                                "content-type": "application/json"
                            },
                            url: "./Json/role.json",
                            isArray: false,
                            timeout: 30000
                        });
                    }
                };
            }]);
    app.service('userService', ['USER', 'ROLE', function (USER, ROLE) {
        this.getUserInfo = function (params, successFunction, failedFunction) {
            return USER.getInfo(JSON.stringify(params)).$promise
                    .then(successFunction, failedFunction);
        };
        this.getRoleInfo = function (params, successFunction, failedFunction) {
            return ROLE.getInfo(JSON.stringify(params))
                    .then(successFunction, failedFunction);
        };
    }]);
    app.controller("userCtrl", ["$scope", "userService", function ($scope, userService) {
        $scope.getUserInfo = function(){
            var params = {
                'USER_ID': 1
            };
            userService.getUserInfo(params)
                    .then(
                    function (response) {
                        $scope.userInfo = response;
                    }
                    , function () {
                        $scope.userInfo = '找不到该用户';
                    }
            );
        };
        $scope.getRoleInfo = function(){
            var params = {
                'ROLE_ID': 1
            };
            userService.getRoleInfo(params)
                    .then(
                    function (response) {
                        $scope.roleInfo = response;
                    }
                    , function () {
                        $scope.roleInfo = '找不到该角色';
                    }
            );
        };
        $scope.getUserInfo();
        $scope.getRoleInfo();
    }]);
</script>
</body>
</html>